//
//  RootTableHeaderView.m
//  VTravel
//
//  Created by lanouhn on 16/8/2.
//  Copyright © 2016年 xiaoYu. All rights reserved.
//

#import "RootTableHeaderView.h"
/** bannerView */
#import "RootHeaderBannerView.h"
/** 自定义button */
#import "CustomButton.h"

#define kScrollView_Hight 200
#define kBtnScrollView_Height 120
#define kBanner_Height 50

@implementation RootTableHeaderView

#pragma mark - 配置轮播图
- (void)creatScrollView{
    // 配置轮播图
    self.sdcycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScrollView_Hight)];
    self.sdcycleScrollView.delegate = self;
    [self addSubview:self.sdcycleScrollView];
}

#pragma mark - 创建用来放button的滚动视图
- (void)creatBtnViewWithBtnTitleArray:(NSArray *)btnTitleArr{
    // 创建用来放button的滚动视图
    UIScrollView *btnScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kScrollView_Hight, kScreenWidth, kBtnScrollView_Height)];
    btnScrollView.pagingEnabled = YES;
    // 设置scrollView的内容区域
    NSInteger count = btnTitleArr.count / 4;
    // 如果button的数量少于4个,或者大于4并且不是4的整数倍,动态设置内容页
    if (count == 0) {
        count = 1;
    }else{
        if ((btnTitleArr.count % 4) != 0) {
            count ++;
        }
    }
    btnScrollView.contentSize = CGSizeMake(count * kScreenWidth, kBtnScrollView_Height);
    // 计算每个button的宽度
    CGFloat button_Width = (kScreenWidth - 5 * KButtonSpace) / 4;
    for (int i = 0; i < btnTitleArr.count; i++) {
        CustomButton *button = [[CustomButton alloc] initWithFrame:CGRectMake(KButtonSpace * (i + 1)+ button_Width * i, 0, button_Width, kBtnScrollView_Height) imageName:btnTitleArr[i] LabelTitle:btnTitleArr[i]];
        if (i >= 4) {
            button.frame = CGRectMake(KButtonSpace * (i + 2)+ button_Width * i, 0, button_Width, kBtnScrollView_Height);
        }
        button.tag = 400 + i;
        [button addTarget:self action:@selector(btnClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [btnScrollView addSubview:button];
    }
    [self addSubview:btnScrollView];
}
#pragma mark - 创建bannerView公告
- (void)creatBannerViewWithBannerDataArr:(NSArray *)bannerDataArr{
    // 创建bannerView公告
    RootHeaderBannerView *bannerView = [[RootHeaderBannerView alloc] initWithFrame:CGRectMake(0, kScrollView_Hight + kBtnScrollView_Height, kScreenWidth, kBanner_Height) bannerDataArray:bannerDataArr];
    // 实现clickBannerBlock
    bannerView.clickBannerBlock = ^(NSInteger index){
        if (self.bannerBlock) {
            self.bannerBlock(index);
        }
        
    };
    [self addSubview:bannerView];
}

#pragma mark - 表头视图上滚动视图上的button的点击事件
- (void)btnClickAction:(CustomButton *)sender{
    if (self.customBtnBlock) {
        self.customBtnBlock(sender.tag - 400);
    }
}

#pragma mark -  实现第一种自定义初始化方法(自定义按钮标题数组和公告数组)
- (instancetype)initWithRootHeaderViewFrame:(CGRect)frame andBtnTitleArray:(NSArray *)btnTitleArr andBannerDataArr:(NSArray *)bannerDataArr{
    if (self = [super initWithFrame:frame]) {
        [self creatScrollView];
        [self creatBtnViewWithBtnTitleArray:btnTitleArr];
        [self creatBannerViewWithBannerDataArr:bannerDataArr];
    }
    return self;
}
#pragma mark - 实现第三种自定义初始化方法(自定义按钮标题数组)
- (instancetype)initWithRootHeaderViewFrame:(CGRect)frame andBtnTitleArray:(NSArray *)btnTitleArr{
    if (self = [super initWithFrame:frame]) {
        
        // 配置轮播图
        self.sdcycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScrollView_Hight)];
        self.sdcycleScrollView.delegate = self;
        [self addSubview:self.sdcycleScrollView];
        
        // 创建用来放button的滚动视图
        UIScrollView *btnScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kScrollView_Hight, kScreenWidth, 150)];
        btnScrollView.pagingEnabled = YES;
        // 设置scrollView的内容区域
        NSInteger count = btnTitleArr.count / 4;
        // 如果button的数量少于4个,或者大于4并且不是4的整数倍,动态设置内容页
        if (count == 0) {
            count = 1;
        }else{
            if ((btnTitleArr.count % 4) != 0) {
                count ++;
            }
        }
        btnScrollView.contentSize = CGSizeMake(count * kScreenWidth, kBtnScrollView_Height);
        // 计算每个button的宽度
        CGFloat button_Width = (kScreenWidth - 6 * 15) / 5;
        for (int i = 0; i < btnTitleArr.count; i++) {
            CustomButton *button = [[CustomButton alloc] initWithFrame:CGRectMake(15 * (i + 1)+ button_Width * i, 0, button_Width, kBtnScrollView_Height) imageName:btnTitleArr[i] LabelTitle:btnTitleArr[i]];
            button.tag = 400 + i;
            [button addTarget:self action:@selector(btnClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
            [btnScrollView addSubview:button];
        }
        [self addSubview:btnScrollView];    }
    return self;
}
#pragma mark - 实现第二种自定义初始化方法(网络按钮标题数组和网络按钮图片数组)
- (instancetype)initWithShipHVwithFram:(CGRect)frame btnTitleArr:(NSArray *)btnTitleArr  btnImageUrlArr:(NSArray *)btnImageUrlArr{
    if (self = [super initWithFrame:frame]) {

        [self creatScrollView];
        
        // 创建用来放button的滚动视图
        UIScrollView *btnScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kScrollView_Hight, kScreenWidth, kBtnScrollView_Height)];
        btnScrollView.pagingEnabled = YES;
        // 设置scrollView的内容区域
        NSInteger count = btnTitleArr.count / 4;
        // 如果button的数量少于4个,或者大于4并且不是4的整数倍,动态设置内容页
        if (count == 0) {
            count = 1;
        }else{
            if ((btnTitleArr.count % 4) != 0) {
                count ++;
            }
        }
        btnScrollView.contentSize = CGSizeMake(count * kScreenWidth, kBtnScrollView_Height);
        // 计算每个button的宽度
        CGFloat button_Width = (kScreenWidth - 5 * KButtonSpace) / 4;
        for (int i = 0; i < btnTitleArr.count; i++) {
            CustomButton *button = [[CustomButton alloc] initWithFrame:CGRectMake(KButtonSpace * (i + 1)+ button_Width * i, 0, button_Width, kBtnScrollView_Height) imageNameUrl:btnImageUrlArr[i] LBTitleStr:btnTitleArr[i]];
            
            if (i >= 4) {
                button.frame = CGRectMake(KButtonSpace * (i + 2)+ button_Width * i, 0, button_Width, kBtnScrollView_Height);
            }
            button.tag = 400 + i;
            [button addTarget:self action:@selector(btnClickAction:) forControlEvents:(UIControlEventTouchUpInside)];
            [btnScrollView addSubview:button];
        }
        [self addSubview:btnScrollView];
    }
    return self;
}

#pragma mark - 点击图片回调
// * 点击图片回调
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    // 安全判断协议存在,且协议方法被实现
    if(_delegate &&[_delegate respondsToSelector:@selector(clickScrollView:andIVIndex:)])
        [_delegate clickScrollView:cycleScrollView andIVIndex:index];
    if (self.scrollViewBlock) {
        self.scrollViewBlock(index);
    }
}
#pragma mark - Block回调方法
/** ClickBannerBlock 和 ClickCustomBtnBlock 回调方法 */
- (void)clickBannerBlock:(ClickBannerBlock)bannerBlock andBtnBlock:(ClickCustomBtnBlock)customBtnBlock{
    self.bannerBlock = bannerBlock;
    self.customBtnBlock = customBtnBlock;
}


@end
